//
//  BAKit_ScreeningView.m
//  BASorterSelectedView
//  筛选
//  Created by Mr_zhang on 2017/9/13.
//  Copyright © 2017年 Mr_zhang. All rights reserved.
//

#import "BAKit_ScreeningView.h"
#import "BAKit_ScreeningModel.h"
#import "BAKit_ScreeningCollectionViewCell.h"
#import "BAKit_ScreeningHeaderView.h"

@interface BAKit_ScreeningView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) UIButton *resetBtn;

@property (nonatomic, strong) UIButton *confirmBtn;
/** 保存 resetBtn 和 confirmBtn 状态的中间按钮 */
@property (nonatomic, strong) UIButton *selectedBtn;

@property (nonatomic, strong) BAKit_ScreeningModel *firstModel;
@property (nonatomic, strong) BAKit_ScreeningModel *secondModel;
@property (nonatomic, strong) BAKit_ScreeningModel *thirdModel;
@property (nonatomic, strong) UIButton *firstBtn;
@property (nonatomic, strong) UIButton *secondBtn;
@property (nonatomic, strong) UIButton *thirdBtn;

@end

@implementation BAKit_ScreeningView

- (NSArray *)dataArray
{
    if (!_dataArray)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"OtherRequire" ofType:@"plist"];
        NSArray *arrs = [[NSDictionary dictionaryWithContentsOfFile:path] objectForKey:@"jobRequire"];
        NSMutableArray *mutaArr = [NSMutableArray array];
        for (NSDictionary *dict in arrs) {
            NSMutableArray *titleArr = [NSMutableArray array];
            for (NSDictionary *titleDict in dict[@"cellTitles"]) {
                BAKit_ScreeningModel *model = [BAKit_ScreeningModel praseDataWithDictionary:titleDict];
                [titleArr addObject:model];
            }
            [mutaArr addObject:titleArr];
        }
        _dataArray = [mutaArr copy];
    }
    return _dataArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setUpSubView];
    }
    return self;
}

- (void)setUpSubView
{
    /** 创建UICollectionView */
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.frame.size.width - 20 * 3)/3, 30);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 15, 10);
    layout.headerReferenceSize = CGSizeMake(self.frame.size.width, 23);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    [self.collectionView registerClass:[BAKit_ScreeningCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[BAKit_ScreeningHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell"];
    self.collectionView.bounces = NO;
    self.collectionView.showsVerticalScrollIndicator = YES;
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:self.collectionView];
    
    /** 创建重置按钮 */
    self.resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.resetBtn.frame = CGRectMake(0, self.frame.size.height - 50, self.frame.size.width / 2, 50);
    self.resetBtn.backgroundColor = [UIColor whiteColor];
    [self.resetBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.resetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.resetBtn setTitle:@"重  置" forState:UIControlStateNormal];
    [self.resetBtn addTarget:self action:@selector(resetMethod:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.resetBtn];
    
    /** 创建确认按钮 */
    self.confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.confirmBtn.frame = CGRectMake(self.frame.size.width / 2, self.frame.size.height - 50, self.frame.size.width / 2, 50);
    self.confirmBtn.selected = YES;
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.confirmBtn.backgroundColor = [UIColor orangeColor];
    [self.confirmBtn setTitle:@"确  认" forState:UIControlStateNormal];
    [self.confirmBtn addTarget:self action:@selector(confirmMethod:) forControlEvents:UIControlEventTouchUpInside];
    self.selectedBtn = self.confirmBtn;
    [self addSubview:self.confirmBtn];
}

- (void)resetMethod:(UIButton *)btn
{
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
    
    btn.backgroundColor = [UIColor orangeColor];
    self.confirmBtn.backgroundColor = [UIColor whiteColor];
    
    // 循环数据源数据，把所有数据的selected为重置为NO
    for (NSArray *arr in self.dataArray) {
        for (BAKit_ScreeningCollectionViewCell *model in arr) {
            model.selected = NO;
        }
    }
    [self.collectionView reloadData];
}

- (void)confirmMethod:(UIButton *)btn
{
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
    
    btn.backgroundColor = [UIColor orangeColor];
    self.resetBtn.backgroundColor = [UIColor whiteColor];
    
    // 循环数据源数据，获取数据的selected为YES的数据
    NSMutableArray *dataSource = [[NSMutableArray alloc] init];
    for (NSArray *arr in self.dataArray) {
        for (BAKit_ScreeningModel *model in arr) {
            if (model.selected == YES){
                [dataSource addObject:model];
            }
            
        }
    }
    // 调用其他要求的Block，返回选中的字段及Id（薪资要求和id，工作年限和id，学历要求和id）的源数组
    if (self.otherRequireBlock)
        self.otherRequireBlock(dataSource);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //NSArray *dataArr = [self.dataArray[section] objectForKey:@"cellTitles"];
    return [self.dataArray[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BAKit_ScreeningCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    BAKit_ScreeningModel *model = [self.dataArray[indexPath.section] objectAtIndex:indexPath.row];
    NSString *buttonTitle = model.title;
    [cell.button setTitle:buttonTitle forState:UIControlStateNormal];
    cell.button.userInteractionEnabled = NO;
    cell.button.selected = model.selected;
    if (model.selected)
        cell.button.backgroundColor = [UIColor orangeColor];
    else
        cell.button.backgroundColor = [UIColor whiteColor];

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BAKit_ScreeningCollectionViewCell *cell = (BAKit_ScreeningCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    if (indexPath.section == 0)
    {
        BAKit_ScreeningModel *model = [self.dataArray[indexPath.section] objectAtIndex:indexPath.row];
        if (!(self.firstModel == model))
            self.firstModel.selected = NO;
        model.selected = !model.selected;
        cell.button.selected = model.selected;
        self.firstModel = model;
    }
    else if (indexPath.section == 1)
    {
        BAKit_ScreeningModel *model = [self.dataArray[indexPath.section] objectAtIndex:indexPath.row];
        if (!(self.secondModel == model))
            self.secondModel.selected = NO;
        model.selected = !model.selected;
        cell.button.selected = !model.selected;
        self.secondModel = model;
    }
    else
    {
        BAKit_ScreeningModel *model = [self.dataArray[indexPath.section] objectAtIndex:indexPath.row];
        if (!(self.thirdModel == model))
            self.thirdModel.selected = NO;
        model.selected = !model.selected;
        cell.button.selected = model.selected;
        self.thirdModel = model;
    }
    
    [self.collectionView reloadData];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    BAKit_ScreeningHeaderView *headReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell" forIndexPath:indexPath];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"OtherRequire" ofType:@"plist"];
    NSArray *arrs = [[NSDictionary dictionaryWithContentsOfFile:path] objectForKey:@"jobRequire"];
    headReusableView.titleLab.text = [arrs[indexPath.section] objectForKey:@"sectionTitle"];
    
    return headReusableView;
}


@end
